/**
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.gitee.huangjianbo.supos.openapi.core.http.request.types;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import io.gitee.huangjianbo.supos.openapi.core.errors.InvalidArgumentsException;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpHeader;
import io.gitee.huangjianbo.supos.openapi.core.http.HttpPath;
import io.gitee.huangjianbo.supos.openapi.core.http.request.MultipartFormDataPair;
import io.gitee.huangjianbo.supos.openapi.core.http.request.RequestArguments;

/**
 * File包装类
 *
 * @author tomcat
 * @version 1.0
 */
public final class RFile implements RequestArguments {
    private static final long serialVersionUID = -2100110276187498957L;

    private final File file;

    public RFile(File file) {
        this.file = file;
    }

    public RFile(String pathname) {
        this.file = new File(pathname);
    }

    public RFile(URI uri) {
        this.file = new File(uri);
    }

    public String getName() {
        return this.file.getName();
    }

    public String getPath() {
        return this.file.getPath();
    }

    public boolean exists() {
        return this.file.exists();
    }

    public boolean notExists() {
        return !exists();
    }

    public boolean isDirectory() {
        return this.file.isDirectory();
    }

    public boolean isFile() {
        return this.file.isFile();
    }

    public long length() {
        return this.file.length();
    }

    public boolean delete() {
        return this.file.delete();
    }

    public void deleteOnExit() {
        this.file.deleteOnExit();
    }

    @Override
    public boolean validate() {
        if (this.file == null || notExists() || this.file.length() == 0) {
            throw new InvalidArgumentsException("file is missing", new FileNotFoundException());
        }
        if (!this.file.isFile()) {
            throw new InvalidArgumentsException("type error, make sure this is a file type: " + this.file.getPath());
        }
        if (!this.file.canRead()) {
            throw new InvalidArgumentsException("file is not readable: " + this.file.getPath());
        }
        return true;
    }

    @Override
    public String toBodyJsonString() {
        return "";
    }

    @Override
    public String toQueryString() {
        return "";
    }

    @Override
    public HttpPath toHttpPath(HttpPath definePath) {
        return definePath;
    }

    @Override
    public HttpHeader toHeader() {
        return HttpHeader.of();
    }

    @Override
    public List<MultipartFormDataPair> toMultipartFormData() {
        return new ArrayList<>();
    }

    public File toFile() {
        return this.file;
    }
}
